using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace RecycleTray
{
    public static class NativeMethods
    {
        #region Constants
        private const int SHERB_NOCONFIRMATION = 0x00000001; // No dialog box confirming the deletion of the objects will be displayed.
        private const int SHERB_NOSOUND = 0x00000004;
        #endregion

        #region Types
        [StructLayout(LayoutKind.Explicit, Size = 20)]
        private struct SHQUERYRBINFO
        {
            [FieldOffset(0)]
            private int _cbSize;
            [FieldOffset(4)]
            private long _i64Size;
            [FieldOffset(12)]
            private long _i64NumItems;

            public int cbSize
            {
                get { return this._cbSize; }
                set { this._cbSize = value; }
            }

            public long i64Size
            {
                get { return this._i64Size; }
            }

            public long i64NumItems
            {
                get { return this._i64NumItems; }
            }
            
            public static bool operator ==(SHQUERYRBINFO s1, SHQUERYRBINFO s2)
            {
                return s1 == s2;
            }

            public static bool operator !=(SHQUERYRBINFO s1, SHQUERYRBINFO s2)
            {
                return s1 != s2;
            }

            public override bool Equals(object obj)
            {
                return base.Equals(obj);
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }
        }
        #endregion

        #region PInvoke
        [DllImport("shell32.dll")]
        private static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);

        [DllImport("shell32.dll")]
        private static extern int SHEmptyRecycleBin(IntPtr hWndOwner, string pszRootPath, uint dwFlags);
        #endregion

        #region Properties
        public static int RecycleBinCount
		{
			get
			{
				NativeMethods.SHQUERYRBINFO sqrbi = new NativeMethods.SHQUERYRBINFO();
                sqrbi.cbSize = Marshal.SizeOf(typeof(NativeMethods.SHQUERYRBINFO));
                NativeMethods.SHQueryRecycleBin(string.Empty, ref sqrbi);
				return Convert.ToInt32(sqrbi.i64NumItems);
			}
		}
        #endregion

        #region Methods
        public static int EmptyRecycleBin()
        {
            return NativeMethods.SHEmptyRecycleBin(IntPtr.Zero, null, SHERB_NOCONFIRMATION | SHERB_NOSOUND);
        }
        #endregion
    }
}
